package com.example.fastshare;

import com.example.fastshare.alljoyn.MainApp;
import com.example.fastshare.alljoyn.Utilities;

import android.app.ActionBar;
import android.app.Application;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements
		ActionBar.TabListener {

	static {
		System.loadLibrary("alljoyn_java");
	}
	public static boolean checkSetting = false;

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;
	private int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		MainApp.m_userInfo = Utilities.loadProfile(this);

		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		mSectionsPagerAdapter = new SectionsPagerAdapter(
				getSupportFragmentManager());

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);

		mViewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						actionBar.setSelectedNavigationItem(position);
					}
				});

		actionBar.addTab(actionBar.newTab().setText("").setTabListener(this)
				.setIcon(R.drawable.action_settings));
		actionBar.addTab(actionBar.newTab().setText("History")
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText("Connection")
				.setTabListener(this));

		mViewPager.setCurrentItem(1);
		if (MainApp.m_userInfo == null) {
			Intent intent = new Intent(this, ProfileActivity.class);
			startActivity(intent);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {

		mViewPager.setCurrentItem(tab.getPosition());
		// this.changeActivity();
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stubreturn
		// super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case R.id.action_settings: {

			// TODO Auto-generated method stub
			quit();

			return true;
		}
		default:
			return super.onOptionsItemSelected(item);

		}
	}

	public void quit() {
		MainApp _mainapp = (MainApp) getApplication();
		_mainapp.quit();
		finish();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
			count++;
			if(count >= 2)
			{
				quit();
			}
			else
			{
				Toast.makeText(this.getApplicationContext(), "Double Back to Exit", Toast.LENGTH_SHORT).show();
				new Handler().postDelayed(new Runnable() {
					 
		            /*
		             * Showing splash screen with a timer. This will be useful when you
		             * want to show case your app logo / company
		             */
		 
		            @Override
		            public void run() {
		                // This method will be executed once the timer is over
		               count = 0;
		            }
		        }, Toast.LENGTH_SHORT);
			}
	}


	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				Fragment settingFragment = new SettingFragment();
				return settingFragment;
			case 1:
				Fragment historyFragment = new HistoryFragment();
				return historyFragment;
			case 2:
				Fragment connectionFragment = new ConnectionsFragment();
				return connectionFragment;
			}
			return null;
		}

		@Override
		public int getCount() {
			return 3;
		}
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentActivity#onMenuItemSelected(int,
	 * android.view.MenuItem)
	 */

}
